#include "TriggerAnalyzer.h"

void TriggerAnalyzer::init(std::string fileName)
{
  TriggerRoad* road = new TriggerRoad(); road->clear();

  TFile dataFile(fileName.c_str(), "READ");
  TTree* p_dataTree = (TTree*)dataFile.Get("single_p");
  TTree* m_dataTree = (TTree*)dataFile.Get("single_m");

  p_dataTree->SetBranchAddress("road", &road);
  m_dataTree->SetBranchAddress("road", &road);

  roads[0].clear();
  for(int i = 0; i < p_dataTree->GetEntries(); i++)
    {
      p_dataTree->GetEntry(i);
      if(road->isValid()) roads[0].push_back(*road);
      road->clear();
    }

  roads[1].clear();
  for(int i = 0; i < m_dataTree->GetEntries(); i++)
    {
      m_dataTree->GetEntry(i);
      if(road->isValid()) roads[1].push_back(*road);
      road->clear();
    }

  std::cout << "From " << fileName << ":" << std::endl;
  std::cout << "Loaded " << roads[0].size() << " positive roads and " << roads[1].size() << " negative roads" << std::endl;
}

bool TriggerAnalyzer::acceptEvent(TriggerRoad& p_road, TriggerRoad& m_road)
{
  std::list<TriggerRoad>::iterator p_iter = std::find(roads[0].begin(), roads[0].end(), p_road);
  std::list<TriggerRoad>::iterator m_iter = std::find(roads[1].begin(), roads[1].end(), m_road);

  if(p_iter != roads[0].end() && p_iter->isValid() && m_iter != roads[1].end() && m_iter->isValid())
    {
      return true;
    }
  return false;
}
